<template>
	<view class="container" v-if="loading">
		<view class="indexLogin">
			<view>获取二维码...</view>
		</view>
		<view class="goback" style="background: #f7f7f7;  color: #e4e7ed; border: none;">推广码</view>
	</view>
</template>
<script>
	export default {
		components: {},
		data() {
			return {
				loading: false,
				successUrl: '',
				nowStamp: (new Date()).getMilliseconds(),
				url: getApp().globalData.url,
				queryExpireApi:"/wxlogin/hasout", 		//TODO 是否登录掉线
				queryAuthUrlApi:"/wxlogin/wxauthurl",  	//TODO 获取授权URL地址
				queryQrcodeApi :"/second/shareQrcode", 	//TODO 收徒临时二维码
				loginAuthParam:false, 					//TODO授权所需参数
				myPageData:null,
				currentStep:undefined,
			};
		}, 
		onShow() {
			console.log("【页面显示】onShow ");
		},
		onLoad(options) {
			//TODO 获取传入的参数内容
			let myParam  = this.parseRequestQuery(); 
			this.myPageData = myParam;
			console.log("收徒启动-onLoadPageFunc 动态参数param=>",this.myPageData);
			//this.$api.msg(JSON.stringify(DevInfo));
			this.onLoadSharePageFunc();
			console.log("加载-onLoadPageFunc 完成");
		},
		methods: {
			onLoadSharePageFunc() {
				let _that = this;
				//TODO 表示已跳微信授权【currentStep=2】
				console.log("加载onLoadSharePageFunc，获取临时推广二维码；并显示" + this.currentStep);
				setTimeout(() => {
					_that.queryTempQrcode(); 
				},1000);
			},
			//TODO  获取推广码，有效期只有一次
			async queryTempQrcode() {
				this.loading = true;
				let queryExpireApi=this.queryExpireApi;
				let mdString = this.myPageData.mdstr || "";
				let subParam = {"mdstr":mdString,"gIndex":this.myPageData.gIndex||0};
				localStorage.removeItem("STEPINDEX");
				localStorage.removeItem("AUTH_JUMPURL");
				localStorage.removeItem("authLoginParam"); //TODO登录参数
				localStorage.removeItem("authJumpLoginUser");
				console.log("【queryAuthExpire】请求接口"+queryExpireApi,subParam);
				if(null===mdString || undefined===mdString|| ""===mdString){
					uni.showToast({ title:"无效链接" ,duration:3000,"icon":'error',mask:true });
					return;
				}
				//3、本地没有就去服务器检测，并登录授权身份；
				this.$api.request({
					url: this.url + queryExpireApi,
					method: 'POST',
					data: subParam,
					success: res => {
						let resData = res.data || null;
						console.log("【queryAuthExpire】请求结果返回" ,resData);
						if (res.code == 200) {
							this.loading = true ;
							let validAuth = resData.validAuth || 0;	//TODO 是否在线【跳微信授权】
							let hasJumpUrl = resData.jumpUrl || "";
							let jumpH5Url = resData.h5PageUrl || "";
							//TODO 登录身份还在线
							if(null !==validAuth && validAuth==1 && hasJumpUrl){
								this.successUrl=hasJumpUrl; 
								localStorage.setItem("authJumpLoginUser",JSON.stringify(resData)); 
								this.loginAuthParam = false;
								console.log("【checkLocalStorageUser】跳转jump成功",hasJumpUrl);
								//TODO 没过期，直接跳到H5页面去；
								this.jumpPage(hasJumpUrl); 
							}else{
								//TODO 需要授权登录
								this.loginAuthParam = {
									"hasAuth":0,
									"authUrl":resData.authUrl,
									"h5PageUrl":resData.h5PageUrl,
									"state":resData.authState ,
								};
								//授权信息存缓存中处理
								localStorage.setItem("authLoginParam",JSON.stringify(this.loginAuthParam));
								localStorage.setItem("STEPINDEX",1);
								//TODO页面跳转
								if(null !== jumpH5Url && undefined !== jumpH5Url && ""!==jumpH5Url){
									localStorage.setItem("AUTH_JUMPURL",jumpH5Url); 
								}
								//TODO 获取授权完整地址；成功后就去进入微信授权页面
								this.findWxAuthService();  
							}
						} else {
							this.$api.msg(res.msg);
							return;
						}
					}
				}); 
			},
			//TODO 请求参数解析值
			parseRequestQuery() {
				const search = new URLSearchParams(window.location.search);
				const res = {};
				search.forEach((value, key) => {
					let arr = res[key];
					if (arr === undefined) {
						arr = res[key] = value;
					} else if (Array.isArray(arr)) {
						arr.push(value);
					} else {
						res[key] = [arr, value];
					}
				});
				return res;
			},
			//TODO 跳转出去页面
			jumpPage(url){
				console.log("【jumpPage】跳转出去",url);
				window.location.href = url; 
				return ;
			}
		}
	};
</script>

<style lang="scss">
	page,.container{
		background: #fff;
		width: 100%;
		min-height: 100%; 
		text-align: center;
		position: relative;
	}
	.bg{
		width: 700upx;
		margin: 20upx auto;
		height: 1304upx;
	}
	.indexLogin {
		background: #e5e5e5;
		min-height: 900px;
		padding: 100px;
		color: #888;
		font-size: 18px;
	}
</style>